<template>
  <el-dialog
    v-el-drag-dialog
    title="规格"
    width="70%"
    :close-on-click-modal="false"
    :visible.sync="visible"
    :modal="false">
    <span style="color: #a0a3a9;font-size: 12px;">提示：未选中的规格将以原价参加活动</span>
    <el-table
      ref="table"
      @cell-click="tableClink"
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        v-if="!flag"
        width="30">
      </el-table-column>
      <el-table-column
        width="170"
        prop="specs"
        label="规格"
        header-align="center"
        align="center">
      </el-table-column>
      <el-table-column
        prop="goodsPrice"
        label="商城售价"
        header-align="center"
        align="center">
      </el-table-column>
      <el-table-column
        prop="price"
        label="商品原价"
        header-align="center"
        align="center">
      </el-table-column>
      <el-table-column
        prop="count"
        label="库存数量"
        header-align="center"
        align="center">
      </el-table-column>
      <el-table-column
        prop="integral"
        label="现金券"
        header-align="center"
        align="center">
      </el-table-column>
      <el-table-column
        prop="goodsCostingPrice"
        label="商品利润"
        header-align="center"
        align="center">
      </el-table-column>
      <el-table-column
        prop="vipPrice"
        header-align="center"
        align="center"
        :label="goodsType ? '活动价（券）':'活动价'">
        <template slot-scope="scope">
          <!-- <span v-if="goodsType" hidden="true">
            {{typeof (scope.row.goodsPrice) === "undefined" ?
                scope.row.goodsPrice = scope.row.integral : scope.row.goodsPrice}}
          </span>
          <span v-if="!goodsType" hidden="true">
            {{typeof (scope.row.goodsPrice) === "undefined" ?
              scope.row.goodsPrice = scope.row.vipPrice : scope.row.goodsPrice}}
          </span> -->
          <!-- <span hidden="true">
            {{typeof (scope.row.goodsPrice) === "undefined" ?
              scope.row.goodsPrice = scope.row.vipPrice : scope.row.goodsPrice}}
          </span>
          <el-input v-model="scope.row.goodsPrice" placeholder="" @change="checkNumber(scope.row.goodsPrice,false)"></el-input> -->
          <el-input v-model="scope.row.vipPrice" placeholder="" @change="checkNumber(scope.row.vipPrice, false, 1, scope.$index)"></el-input>
        </template>
      </el-table-column>
      <el-table-column
        v-if="!goodsType"
        prop="activityGoodsCostingPrice"
        header-align="center"
        align="center"
        label="活动利润">
        <template slot-scope="scope">
          <span hidden="true">{{typeof (scope.row.activityGoodsCostingPrice) === "undefined"? scope.row.activityGoodsCostingPrice = scope.row.goodsCostingPrice : scope.row.activityGoodsCostingPrice}}</span>
          <el-input v-model="scope.row.activityGoodsCostingPrice" placeholder="" @change="checkNumber(scope.row.activityGoodsCostingPrice,false, 2, scope.$index)"></el-input>
        </template>
      </el-table-column>
      <el-table-column
        prop="goodsInventory"
        header-align="center"
        align="center"
        label="活动库存">
        <template slot-scope="scope">
          <span hidden="true">{{typeof (scope.row.goodsInventory) === "undefined" ? scope.row.goodsInventory = scope.row.count : scope.row.goodsInventory}}</span>
          <el-input v-model="scope.row.goodsInventory" @change="checkInventory(scope.row.goodsInventory,scope.row.count)"></el-input>
        </template>
      </el-table-column>
      <el-table-column
        prop="giveIntegral"
        header-align="center"
        align="center"
        label="活动赠积分">
        <template slot-scope="scope">
          <!-- <span v-if="!goodsType" hidden="true">
            {{typeof (scope.row.sendIntegral) === "undefined" ?
                scope.row.sendIntegral = scope.row.integral : scope.row.sendIntegral}}
          </span> -->
          <span v-if="goodsType" hidden="true">{{typeof (scope.row.giveIntegral) === "undefined" ? scope.row.giveIntegral = 0 : scope.row.giveIntegral}}</span>
          <el-input v-model="scope.row.giveIntegral" disabled=true placeholder="" @change="checkNumber(scope.row.giveIntegral,true, 2, scope.$index)" ></el-input>
        </template>
      </el-table-column>
      <el-table-column
        prop="integral"
        header-align="center"
        align="center"
        label="兑换积分">
        <template slot-scope="scope">
          <!-- <span hidden="true">{{typeof (scope.row.integral) === "undefined" ? scope.row.integral = 0 : scope.row.integral}}</span> -->
          <el-input v-model="scope.row.activityIntegral" @change="checkNumber(scope.row.activityIntegral, true, 2, scope.$index)"></el-input>
        </template>
      </el-table-column>
      <el-table-column
        prop="buyerPrice"
        header-align="center"
        align="center"
        label="商品进价">
        <template slot-scope="scope">
          <!-- <span hidden="true">{{typeof (scope.row.giveIntegral) === "undefined" ? scope.row.giveIntegral = scope.row.count : scope.row.giveIntegral}}</span> -->
          <el-input v-model="scope.row.buyerPrice" @change="checkNumber(scope.row.buyerPrice, false, 2, scope.$index)"></el-input>
        </template>
      </el-table-column>
      <el-table-column
        prop="weight"
        header-align="center"
        align="center"
        label="重量">
        <template slot-scope="scope">
          <el-input v-model="scope.row.weight"></el-input>
        </template>
      </el-table-column>
      <el-table-column
        prop="startExpDate"
        header-align="center"
        align="center"
        width="150"
        label="开始时间"
        v-if="false">
        <template slot-scope="scope">
          <el-input v-model="scope.row.startExpDate"></el-input>
        </template>
      </el-table-column>
      <el-table-column
        prop="endExpDate"
        header-align="center"
        align="center"
        width="150"
        label="结束时间"
        v-if="false">
        <template slot-scope="scope">
          <el-input v-model="scope.row.endExpDate"></el-input>
        </template>
      </el-table-column>
    </el-table>

    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="addSelectSpecs()" :disabled="checkFlag">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data: function () {
      return {
        checkFlag: false,
        flag: false,
        visible: false,
        goodsType: null,
        dataList: [],
        dataListLoading: false,
        id: null,
        dataListSelections: [],
        integralType: 1
      }
    },
    methods: {
      checkInventory (goodsInventory, count) {
        if (parseInt(goodsInventory) > parseInt(count)) {
          this.checkFlag = true
          this.$message.error('活动库存不可以大于原有库存')
        }
        if (Math.sign(Number.parseFloat(goodsInventory)) === -1 || !Number.isInteger(Number.parseFloat(goodsInventory))) {
          this.checkFlag = true
          this.$message.error('数据不合法')
        } else {
          this.checkFlag = false
        }
      },
      checkNumber (value, flag, type, index) {
        let reg = ''
        if (flag) {
          // 整数
          reg = /^\d+$/
        } else {
          // 小数
          reg = /^(^[1-9](\d+)?(\.\d{1,2})?$)|(^0$)|(^\d\.\d{1,}$)$/
        }
        if (!reg.test(value)) {
          this.$message.error('数据不合法')
          this.checkFlag = true
        } else {
          this.checkFlag = false
        }
        debugger
        if (type === 1) {
          if (this.integralType === '1') {
            if ((/^[0-9]*[1-9][0-9]*$/.test(value))) {
              this.dataList[index].giveIntegral = Number(value)
            } else {
              let price = value.split('.')
              this.dataList[index].giveIntegral = Number(price[0])
            }
          } else {
            if ((/^[0-9]*[1-9][0-9]*$/.test(value))) {
              let endPrice = Number(value) / 2 + ''
              if ((/^[0-9]*[1-9][0-9]*$/.test(endPrice))) {
                this.dataList[index].giveIntegral = Number(endPrice)
              } else {
                this.dataList[index].giveIntegral = Number(endPrice.split('.')[0])
              }
            } else {
              let startPrice = (value / 2) + ''
              let price = startPrice.split('.')
              this.dataList[index].giveIntegral = Number(price[0])
            }
          }
        }
      },
      init (id, info, flag, goodsType, integralType) {
        debugger
        this.integralType = 1
        this.integralType = integralType
        this.dataList = []
        this.visible = true
        if (flag == null && info != undefined && info.length > 0) {
          for (let i = 0; i < info.length; i++) {
            info[i].goodsPrice = info[i].vipPrice
            // if (info[i].goodsPrice != null && info[i].goodsPrice !== '') {
            //   if (integralType === '1') {
            //     if ((/^[0-9]*[1-9][0-9]*$/.test(info[i].goodsPrice))) {
            //       info[i].sendIntegral = info[i].goodsPrice
            //     } else {
            //       let price = info[i].goodsPrice.split('.')
            //       info[i].sendIntegral = Number(price[0])
            //     }
            //   } else {
            //     if ((/^[0-9]*[1-9][0-9]*$/.test(info[i].goodsPrice))) {
            //       let endPrice = Number(info[i].goodsPrice) / 2 + ''
            //       if ((/^[0-9]*[1-9][0-9]*$/.test(endPrice))) {
            //         info[i].sendIntegral = Number(endPrice)
            //       } else {
            //         info[i].sendIntegral = Number(endPrice.split('.')[0])
            //       }
            //     } else {
            //       let startPrice = (price / 2) + ''
            //       let price = startPrice.split('.')
            //       info[i].sendIntegral = Number(price[0])
            //     }
            //   }
            //   // 计算兑换积分价格
            //   let exchange = info[i].goodsPrice * 14.28
            //   if ((/^[0-9]*[1-9][0-9]*$/.test(exchange))) {
            //     info[i].integral = exchange
            //   } else {
            //     let a = exchange + ''
            //     let b = a.split('.')
            //     info[i].integral = Number(b[0]) + 1
            //   }
            // } else {
              if (integralType === '1') {
                if ((/^[0-9]*[1-9][0-9]*$/.test(info[i].vipPrice))) {
                  info[i].giveIntegral = Number(info[i].vipPrice)
                } else {
                  // let price = info[i].vipPrice.split('.')
                  // info[i].giveIntegral = Number(price[0])
                  info[i].giveIntegral = Math.floor(info[i].vipPrice)
                }
              } else {
                if ((/^[0-9]*[1-9][0-9]*$/.test(info[i].vipPrice))) {
                  let endPrice = Number(info[i].vipPrice) / 2 + ''
                  if ((/^[0-9]*[1-9][0-9]*$/.test(endPrice))) {
                    info[i].giveIntegral = Number(endPrice)
                  } else {
                    // info[i].giveIntegral = Number(endPrice.split('.')[0])
                    info[i].giveIntegral = Math.floor(endPrice)
                  }
                } else {
                  let startPrice = (info[i].vipPrice / 2) + ''
                  // let price = startPrice.split('.')
                  // info[i].giveIntegral = Number(price[0])
                  info[i].giveIntegral = Math.floor(startPrice)
                }
              }
              // 计算兑换积分价格
              // let exchange = info[i].buyPrice * 14.28
              debugger
              info[i].activityIntegral = Math.ceil(info[i].integral)
              // if ((/^[0-9]*[1-9][0-9]*$/.test(exchange))) {
              //   info[i].integral = exchange
              // } else {
              //   info[i].integral = Math.ceil(exchange)
              //   // let a = exchange + ''
              //   // let b = a.split('.')
              //   // info[i].integral = Number(b[0]) + 1
              // }
            }
          // }
        }
        this.dataList = info
        this.id = id
        this.goodsType = goodsType
        this.flag = flag
      },
      tableClink (row, column, cell, event) {
        if (column.label === '活动赠券' || column.label === '活动库存' || column.label === '活动利润' || column.label === '活动价' || column.label === '活动价（券）') {
          this.$refs.table.toggleRowSelection(row, true)
        }
      },
      // 表单提交
      addSelectSpecs: function () {
        let selectId = this.dataListSelections.map(item => {
            return item.id
        })
        if (selectId.length === 0) {
          this.$message.error('未选择活动商品规格')
          return
        }
        this.dataListLoading = true
        this.visible = false

      // 最大的活动利润
        let maxCostingPrice = 0
      // 最小的活动价
        let minPrice = 99999999
        let goodsIntegral = 0
        if (this.flag) {
          // 后端展示数据
          // let activityGoodsInventoryDetail = JSON.parse(JSON.stringify(this.dataList))
          let mainSendIntegral = 0
          // 接口使用的数据 进行替换
          // for (let x = 0; x < this.dataList.length; x++) {
          //   if (mainSendIntegral > this.dataList[x].giveIntegral) {
          //     mainSendIntegral = this.dataList[x].giveIntegral
          //   } else {
          //     mainSendIntegral = this.dataList[x].giveIntegral
          //   }
          //   let tempPrice = 0
          //   if (Number.parseInt(this.dataList[x].priceType) === 1) {
          //     // 兑换
          //     this.dataList[x].integral = this.dataList[x].goodsPrice

          //     tempPrice = this.dataList[x].integral
          //   } else {
          //     this.dataList[x].vipPrice = this.dataList[x].goodsPrice
          //     // this.dataList[x].integral = this.dataList[x].sendIntegral
          //     this.dataList[x].goodsCostingPrice = this.dataList[x].activityGoodsCostingPrice

          //     tempPrice = this.dataList[x].vipPrice
          //   }
          //   this.dataList[x].count = this.dataList[x].goodsInventory

          //   // 计算最大的活动利润
          //   if (Number.parseInt(this.dataList[x].activityGoodsCostingPrice) > Number.parseInt(maxCostingPrice)) {
          //     maxCostingPrice = this.dataList[x].activityGoodsCostingPrice
          //   }

          //   // 计算最小的价格
          //   if (Number.parseInt(tempPrice) < Number.parseInt(minPrice)) {
          //     minPrice = tempPrice
          //     if (Number.parseInt(this.dataList[x].priceType) !== 1) {
          //       goodsIntegral = this.dataList[x].giveIntegral
          //     }
          //   }
          //   debugger
          //   let showPrice1 = this.dataList[x].activityGoodsCostingPrice * 0.7
          //   if ((/^[0-9]*[1-9][0-9]*$/.test(showPrice1))) {
          //     this.dataList[x].showCostingPrice = showPrice1
          //   } else {
          //     // let price = (this.dataList[x].activityGoodsCostingPrice * 0.7).split('.')
          //     console.log(Math.floor(showPrice1))
          //     this.dataList[x].showCostingPrice = Math.floor(showPrice1)
          //   }
          // }
          // 商品需要的积分数量
          let minGoodsIntegral = 0
          // 商品市场价格
          let minGoodsPrice = 0
          // 活动商品会员价格
          let minVipPrice = 0
          // 活动商品送券数量
          let minSendIntegral = 0
          // 商品目前总库存
          let goodsInventoryNum = 0
          // 利润
          let activityGoodsCostingPrice = 0
          debugger
          for (var y = 0; y < this.dataList.length; y++) {
            this.dataList[y].integral = this.dataList[y].activityIntegral

            goodsInventoryNum = Number(goodsInventoryNum) + Number(this.dataList[y].goodsInventory)
            this.dataList[y].showCostingPrice = Math.floor(this.dataList[y].activityGoodsCostingPrice * 0.7)
            if (minVipPrice === 0) {
              minVipPrice = this.dataList[y].vipPrice
              minGoodsIntegral = this.dataList[y].integral
              minGoodsPrice = this.dataList[y].price
              minSendIntegral = this.dataList[y].giveIntegral
              activityGoodsCostingPrice = this.dataList[y].activityGoodsCostingPrice
            } else {
              if (Number(minVipPrice) > Number(this.dataList[y].vipPrice)) {
                minVipPrice = this.dataList[y].vipPrice
                minGoodsIntegral = this.dataList[y].integral
                minGoodsPrice = this.dataList[y].price
                minSendIntegral = this.dataList[y].giveIntegral
                activityGoodsCostingPrice = this.dataList[y].activityGoodsCostingPrice
              } 
            }
          }
          console.log(this.dataList)
          let activityGoodsInventoryDetail = JSON.parse(JSON.stringify(this.dataList))
          // 修改时
          this.$http({
            url: this.$http.adornUrl(`/activity/spikeGoods/modify`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.id,
              'goodsInventoryDetail': JSON.stringify(this.dataList),
              'priceType': this.goodsType ? 1 : 2,
              'goodsCostingPrice': Math.floor(activityGoodsCostingPrice),
              'goodsIntegral': minGoodsIntegral,
              'minPrice': minVipPrice,
              'activityGoodsInventoryDetail': JSON.stringify(activityGoodsInventoryDetail),
              'sendIntegral': minSendIntegral,
              'goodsInventory': goodsInventoryNum,
              'goodsPrice': minGoodsPrice,
              'goodsCostingPrice': activityGoodsCostingPrice
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.$parent.getGoodsDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        } else {
          // 添加规格时
          let ids = this.dataListSelections.map(item => {
            return item.id
          })
          if (ids.length === 0) {
            this.visible = false
            return
          }
          for (let j = 0; j < this.dataList.length; j++) {
            // 展示利润 是活动利润的70%
            debugger
            let showPrice = this.dataList[j].activityGoodsCostingPrice * 0.7
            if ((/^[0-9]*[1-9][0-9]*$/.test(showPrice))) {
              this.dataList[j].showCostingPrice = showPrice
            } else {
              this.dataList[j].showCostingPrice = Math.floor(showPrice)
            }
            // this.dataList[j].showCostingPrice = Math.ceil(this.dataList[j].activityGoodsCostingPrice * 0.7)
            // if (ids.indexOf(this.dataList[j].id) === -1) {
            //   this.dataList[j].activityGoodsCostingPrice = this.dataList[j].goodsCostingPrice
            //   this.dataList[j].sendIntegral = this.dataList[j].integral
            //   this.dataList[j].goodsInventory = this.dataList[j].count
            //   if (Number.parseInt(this.dataList[j].priceType) === 1) {
            //     this.dataList[j].goodsPrice = this.dataList[j].sendIntegral
            //   } else {
            //     this.dataList[j].goodsPrice = this.dataList[j].vipPrice
            //   }
            // }
          }
          // 后端展示数据
          let activityGoodsInventoryDetail = JSON.parse(JSON.stringify(this.dataList))
          debugger
        // 接口使用的数据 进行替换
          // for (let x = 0; x < this.dataList.length; x++) {
          //   let tempPrice = 0
          //   if (Number.parseInt(this.dataList[x].priceType) === 1) {
          //     debugger
          //     this.dataList[x].integral = this.dataList[x].goodsPrice

          //     tempPrice = this.dataList[x].integral
          //   } else {
          //     this.dataList[x].vipPrice = this.dataList[x].goodsPrice
          //     this.dataList[x].integral = this.dataList[x].sendIntegral
          //     this.dataList[x].goodsCostingPrice = this.dataList[x].activityGoodsCostingPrice

          //     tempPrice = this.dataList[x].vipPrice
          //   }
          //   this.dataList[x].count = this.dataList[x].goodsInventory

          //   // 计算最大的活动利润
          //   if (Number.parseInt(this.dataList[x].activityGoodsCostingPrice) > Number.parseInt(maxCostingPrice)) {
          //     maxCostingPrice = this.dataList[x].activityGoodsCostingPrice
          //   }
          //   // 计算最小的价格
          //   if (Number.parseInt(tempPrice) < Number.parseInt(minPrice)) {
          //     minPrice = tempPrice
          //     if (Number.parseInt(this.dataList[x].priceType) !== 1) {
          //       goodsIntegral = this.dataList[x].sendIntegral
          //     }
          //   }
          // }
          // for (let x = 0; x < this.dataList.length; x++) {
          //   let tempPrice = 0
          //   if (Number.parseInt(this.dataList[x].priceType) === 1) {

          //     tempPrice = this.dataList[x].goodsPrice
          //   } else {
          //     tempPrice = this.dataList[x].goodsPrice
          //   }
          //   this.dataList[x].count = this.dataList[x].goodsInventory

          //   // 计算最大的活动利润
          //   if (Number.parseInt(this.dataList[x].activityGoodsCostingPrice) > Number.parseInt(maxCostingPrice)) {
          //     maxCostingPrice = this.dataList[x].activityGoodsCostingPrice
          //   }
          //   // 计算最小的价格
          //   if (Number.parseInt(tempPrice) < Number.parseInt(minPrice)) {
          //     minPrice = tempPrice
          //     if (Number.parseInt(this.dataList[x].priceType) !== 1) {
          //       goodsIntegral = this.dataList[x].giveIntegral
          //     }
          //   }
          // }
          console.log(activityGoodsInventoryDetail)
          // 商品需要的积分数量
          let minGoodsIntegral1 = 0
          // 商品市场价格
          let minGoodsPrice1 = 0
          // 活动商品会员价格
          let minVipPrice1 = 0
          // 活动商品送券数量
          let minSendIntegral1 = 0
          // 商品目前总库存
          let goodsInventoryNum1 = 0
          // 利润
          let activityGoodsCostingPrice1 = 0
          debugger
          for (var y = 0; y < this.dataList.length; y++) {
            goodsInventoryNum1 = Number(goodsInventoryNum1) + Number(this.dataList[y].goodsInventory)
            this.dataList[y].showCostingPrice = Math.floor(this.dataList[y].activityGoodsCostingPrice * 0.7)
            if (minVipPrice1 === 0) {
              minVipPrice1 = this.dataList[y].vipPrice
              minGoodsIntegral1 = this.dataList[y].activityIntegral
              minGoodsPrice1 = this.dataList[y].price
              minSendIntegral1 = this.dataList[y].giveIntegral
              activityGoodsCostingPrice1 = this.dataList[y].activityGoodsCostingPrice
            } else {
              if (Number(minVipPrice1) > Number(this.dataList[y].vipPrice)) {
                minVipPrice1 = this.dataList[y].vipPrice
                minGoodsIntegral1 = this.dataList[y].activityIntegral
                minGoodsPrice1 = this.dataList[y].price
                minSendIntegral1 = this.dataList[y].giveIntegral
                activityGoodsCostingPrice1 = this.dataList[y].activityGoodsCostingPrice
              } 
            }
          }
          debugger
          this.$emit('info', this.id, this.dataList, this.dataList, activityGoodsCostingPrice1, minGoodsPrice1, minGoodsIntegral1, minSendIntegral1, minVipPrice1, goodsInventoryNum1)
        }
        this.dataListLoading = false
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      }
    }
  }
</script>
